//
//  Main_Title_View_WX.m
//  ZITHER
//
//  Created by weixin on 2019/2/19.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Main_Title_View_WX.h"
#import "Ugc_Find_VC_WX.h"
#import "MainTopicViewController.h"
#import "Ugc_Release_VC_WX_V4.h"
#import "MeVC.h"
#import "MeRegisterLoginVC.h"

@implementation Main_Title_View_WX


-(void)setState:(MainTitleVState)state{
    _state = state;
    self.index = 0;

    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        self.backgroundColor = Main_Token_Model_MANAGER.backgroundColor ;
    }else{
        self.backgroundColor = [UIColor whiteColor];
    }
    
    self.fontSelect = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
    self.fontNormal = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];

    switch (state) {
        case MainTitleVStateNormal:
            //默认样式
            break;
        case MainTitleVStateleft:
            //靠左对齐样式
            [self setTitleLeft];
            break;
        case MainTitleVStateRight:
            //靠左对齐样式
            [self setTitleRight];
            break;
        case MainTitleVStateUGC:
            //靠左对齐样式
            [self setTitleUGC];
            break;
        case MainTitleVStateLogo:
            //靠左对齐样式
            [self setLogo];
            break;
        case MainTitleVStateTaizi:
            //靠左对齐样式
            [self setTaizi];
            break;
        default:
            break;
    }
}

-(void)setTaizi{
    NSArray *arr =  [self subviews];
    for (UIView *subView in arr) {
        [subView removeFromSuperview];
    }
    
    for (int i = 0; i<self.arrMNormal.count; i++) {
        
        BaseBtn *btn = [BaseBtn initCustomBtnTitlestype:@[self.arrMNormal[i]] color:@[HexColor(0x333333),HexColor(0x333333)]];
        
        btn.backgroundColor = [UIColor clearColor];
        btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
        btn.tag = i+999;
        [[btn addTag]subscribeNext:^(id  _Nullable x) {
//            [self changeBtn:btn.tag-999];
            if (self.blcokIndex) {
                self.blcokIndex(btn.tag-999);
            }
        }];
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(30);
            make.bottom.equalTo(self).offset(-5);
            make.left.equalTo(self).offset(5+70*i);
        }];
    }
}

-(void)setLogo{
    BaseImageV *Logo = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        Logo.image =[UIImage imageNamed:@"WX我的"];
        [[Logo addTag]subscribeNext:^(id  _Nullable x) {
            
            if (kUSER_INFO_MANAGER.userIsLogin) {
                MeVC *vc = [[MeVC alloc]init];
                [self.getNav pushViewController:vc animated:YES];
            }else{
                MeRegisterLoginVC *vc = [[MeRegisterLoginVC alloc]init];
                [self.getNav pushViewController:vc animated:YES];
            }
            
            
        }];
    }else{
        Logo.image = [[UIImage imageNamed:@"文字logo"] imageWithColor:[UIColor whiteColor]];
    }

    
    Logo.backgroundColor = [UIColor clearColor];

    
    BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    view.backgroundColor = [UIColor blackColor];
    [view setCornerRadiusWithRadius:21 isMasks:YES];
    
    
    [self addSubview:view];
    [self addSubview:Logo];
    
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        [Logo mas_makeConstraints:^(MASConstraintMaker *make) {
            //                make.height.mas_equalTo(20);
            //                make.width.mas_equalTo(65);
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(40);
            make.bottom.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(15);
        }];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(42);
            make.width.mas_equalTo(42);
            make.centerX.equalTo(Logo.mas_centerX).offset(0);
            make.centerY.equalTo(Logo.mas_centerY).offset(0);
        }];
        [view setCornerRadiusWithRadius:21 isMasks:YES];
        
    }else{
        [Logo mas_makeConstraints:^(MASConstraintMaker *make) {
            //                make.height.mas_equalTo(20);
            //                make.width.mas_equalTo(65);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(20);
            make.bottom.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(15);
        }];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(30);
            make.centerX.equalTo(Logo.mas_centerX).offset(0);
            make.centerY.equalTo(Logo.mas_centerY).offset(0);
        }];
        [view setCornerRadiusWithRadius:15 isMasks:YES];
        
    }
    
    
    
}

-(void)setTitleUGC{

    BaseBtn *btn = [BaseBtn initCustomBtn:@[@"动态_发布"]];
    btn.backgroundColor = [UIColor clearColor];
    [btn setLocalImage:@"动态_发布"];
    [[btn addTag]subscribeNext:^(id  _Nullable x) {

//            [DEFAULReleaseV cleanData];
//            DEFAULReleaseV.frame = CGRectMake(0, kScreenH, kScreenW, kScreenH);
        
            if (kUSER_INFO_MANAGER.userIsLogin) {
                
                Ugc_Release_VC_WX_V4 * vc= [[Ugc_Release_VC_WX_V4 alloc]init];
                [self.getNav pushViewController:vc animated:YES];
                
//                [UIView animateWithDuration:0.3 animations:^{
//                    DEFAULReleaseV.frame = CGRectMake(0, -20, kScreenW, kScreenH);
//                } completion:^(BOOL finished) {
//                    [UIView animateWithDuration:0.2 animations:^{
//                        DEFAULReleaseV.frame = CGRectMake(0, 0, kScreenW, kScreenH);
//                    }];
//                }];
//                [[UIApplication sharedApplication].keyWindow addSubview:DEFAULReleaseV];
//
//
//                [self.viewController.view addSubview:DEFAULReleaseV];
//                [self.viewController.tabBarController.tabBar setHidden:YES];
//                DEFAULReleaseV.vc = self.viewController;
                
            }else{
                [Tools pushSinge];
            }
    }];
    [self addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
            make.width.mas_equalTo(30);
            make.right.equalTo(self).offset(-10);
        }else{
            make.width.mas_equalTo(30);
            make.right.equalTo(self).offset(-5);
        }
        
        make.height.mas_equalTo(30);
        make.bottom.equalTo(self).offset(-5);
    }];
     
    BaseImageV *Logo = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        Logo.image = [UIImage imageNamed:@"WX我的"];
        [[Logo addTag]subscribeNext:^(id  _Nullable x) {
            if (kUSER_INFO_MANAGER.userIsLogin) {
                MeVC *vc = [[MeVC alloc]init];
                [self.getNav pushViewController:vc animated:YES];
            }else{
                MeRegisterLoginVC *vc = [[MeRegisterLoginVC alloc]init];
                [self.getNav pushViewController:vc animated:YES];
            }
        }];
    }else{
        Logo.image = [[UIImage imageNamed:@"文字logo"] imageWithColor:[UIColor whiteColor]];
    }
    Logo.backgroundColor = [UIColor clearColor];
   
    BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    view.backgroundColor = [UIColor blackColor];
    [view setCornerRadiusWithRadius:21 isMasks:YES];
    
    
    [self addSubview:view];
    [self addSubview:Logo];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        [Logo mas_makeConstraints:^(MASConstraintMaker *make) {
            //                make.height.mas_equalTo(20);
            //                make.width.mas_equalTo(65);
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(40);
            make.bottom.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(15);
        }];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(42);
            make.width.mas_equalTo(42);
            make.centerX.equalTo(Logo.mas_centerX).offset(0);
            make.centerY.equalTo(Logo.mas_centerY).offset(0);
        }];
        [view setCornerRadiusWithRadius:21 isMasks:YES];
        
    }else{
        [Logo mas_makeConstraints:^(MASConstraintMaker *make) {
            //                make.height.mas_equalTo(20);
            //                make.width.mas_equalTo(65);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(20);
            make.bottom.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(15);
        }];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(30);
            make.centerX.equalTo(Logo.mas_centerX).offset(0);
            make.centerY.equalTo(Logo.mas_centerY).offset(0);
        }];
        [view setCornerRadiusWithRadius:15 isMasks:YES];
        
    }
        
}

-(void)setTitleRight{
    
    NSArray *arr =  [self subviews];
    for (UIView *subView in arr) {
        [subView removeFromSuperview];
    }
    
    for (NSInteger i = self.arrMNormal.count-1; i>=0; i--) {
        
        BaseBtn *btn = [BaseBtn initCustomBtnTitlestype:@[self.arrMNormal[i]] color:@[HexColor(0x999999),HexColor(0x333333)]];
        btn.tag = i+999;
        btn.backgroundColor = [UIColor clearColor];

        [[btn addTag]subscribeNext:^(id  _Nullable x) {
            [self changeBtn:btn.tag-999];
            if (self.blcokIndex) {
                self.blcokIndex(btn.tag-999);
            }
        }];
        [self addSubview:btn];
        NSInteger num = self.arrMNormal.count-1-i;
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(30);
//            make.height.mas_equalTo(16);
//            make.width.mas_equalTo(18);
            make.bottom.equalTo(self).offset(-5);
            make.right.equalTo(self).offset(-(5+60*num));
        }];
        
        if (i == 0) {

            BaseImageV *Logo = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
            if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
                Logo.image =[UIImage imageNamed:@"WX我的"];
                [[Logo addTag]subscribeNext:^(id  _Nullable x) {
                    if (kUSER_INFO_MANAGER.userIsLogin) {
                        MeVC *vc = [[MeVC alloc]init];
                        [self.getNav pushViewController:vc animated:YES];
                    }else{
                        MeRegisterLoginVC *vc = [[MeRegisterLoginVC alloc]init];
                        [self.getNav pushViewController:vc animated:YES];
                    }
                }];
            }else{
                Logo.image = [[UIImage imageNamed:@"文字logo"] imageWithColor:[UIColor whiteColor]];
            }
            Logo.backgroundColor = [UIColor clearColor];
   
            BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
            view.backgroundColor = [UIColor blackColor];
            
        
            [self addSubview:view];
            [self addSubview:Logo];
            if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
                [Logo mas_makeConstraints:^(MASConstraintMaker *make) {
                    //                make.height.mas_equalTo(20);
                    //                make.width.mas_equalTo(65);
                    make.height.mas_equalTo(40);
                    make.width.mas_equalTo(40);
                    make.bottom.equalTo(self).offset(-10);
                    make.left.equalTo(self).offset(15);
                }];
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(42);
                    make.width.mas_equalTo(42);
                    make.centerX.equalTo(Logo.mas_centerX).offset(0);
                    make.centerY.equalTo(Logo.mas_centerY).offset(0);
                }];
                [view setCornerRadiusWithRadius:21 isMasks:YES];

            }else{
                [Logo mas_makeConstraints:^(MASConstraintMaker *make) {
                    //                make.height.mas_equalTo(20);
                    //                make.width.mas_equalTo(65);
                    make.height.mas_equalTo(20);
                    make.width.mas_equalTo(20);
                    make.bottom.equalTo(self).offset(-10);
                    make.left.equalTo(self).offset(15);
                }];
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(30);
                    make.width.mas_equalTo(30);
                    make.centerX.equalTo(Logo.mas_centerX).offset(0);
                    make.centerY.equalTo(Logo.mas_centerY).offset(0);
                }];
                [view setCornerRadiusWithRadius:15 isMasks:YES];

            }
          
      
   
        }
    }
    
    [self addSubview:self.blackView];
    [self.blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(2);
        make.bottom.equalTo(self).offset(0);
        make.left.equalTo(self.mas_left).offset(2);
        make.right.equalTo(self.mas_right).offset(-2);
    }];
    [self changeBtn:0];
    
    

//    [self addSubview:self.BtnFind];
//    [self.BtnFind mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(40);
//        make.width.mas_equalTo(45);
//        make.bottom.equalTo(self).offset(0);
//        make.right.equalTo(self.mas_right).offset(-10);
//    }];
    
//    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"Main_Title_View_WX_BtnNew"]) {
//        [self addSubview:self.BtnNew];
//        [self.BtnNew mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(40);
//            make.width.mas_equalTo(40);
//            make.bottom.equalTo(self).offset(0);
//            make.right.equalTo(self.BtnFind.mas_left).offset(3);
//        }];
//    }
    
}

-(void)setTitleLeft{
    
    NSArray *arr =  [self subviews];
    for (UIView *subView in arr) {
        [subView removeFromSuperview];
    }
    
    for (int i = 0; i<self.arrMNormal.count; i++) {
        
        
        
        BaseBtn *btn = [BaseBtn initCustomBtnTitlestype:@[self.arrMNormal[i]] color:@[HexColor(0x999999),HexColor(0x333333)]];
        
        btn.backgroundColor = [UIColor clearColor];

        btn.tag = i+999;
        [[btn addTag]subscribeNext:^(id  _Nullable x) {
            [self changeBtn:btn.tag-999];
            if (self.blcokIndex) {
                self.blcokIndex(btn.tag-999);
            }
        }];
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(30);
            make.bottom.equalTo(self).offset(-5);
            make.left.equalTo(self).offset(5+60*i);
        }];
    }
    
    
    [self addSubview:self.blackView];
    [self.blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(2);
        make.bottom.equalTo(self).offset(0);
        make.left.equalTo(self.mas_left).offset(2);
        make.right.equalTo(self.mas_right).offset(-2);
    }];
    [self changeBtn:0];
    
    
    [self addSubview:self.BtnFind];
    [self.BtnFind mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(45);
        make.bottom.equalTo(self).offset(0);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
   
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"Main_Title_View_WX_BtnNew"]) {
        [self addSubview:self.BtnNew];
        [self.BtnNew mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(40);
            make.width.mas_equalTo(40);
            make.bottom.equalTo(self).offset(0);
            make.right.equalTo(self.BtnFind.mas_left).offset(3);
        }];
    }
    
}

-(void)changeBtn:(NSInteger)index{
    NSLog(@"index = %ld",index);
    self.index = index;
    for (int i = 0; i<self.arrMNormal.count; i++) {
        BaseBtn *btn = [self viewWithTag:i+999];
        if (index == i) {
            btn.selected = YES;
            btn.titleLabel.font = self.fontSelect;
            [self.blackView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(2);
                make.bottom.equalTo(self).offset(0);
                make.left.equalTo(btn.mas_left).offset(10);
                make.right.equalTo(btn.mas_right).offset(-10);
            }];
            [UIView animateWithDuration:0.3 animations:^{
                [self layoutIfNeeded];
            }];
    
        }else{
            btn.selected = NO;
            btn.titleLabel.font = self.fontNormal;
        }
    }
}

-(BaseView *)blackView{
    if (!_blackView) {
        _blackView = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _blackView.backgroundColor = HexColor(0x333333);
        
    }
    return _blackView;
}
-(BaseBtn *)BtnFind{
    if (!_BtnFind) {
        _BtnFind = [BaseBtn initCustomBtnImageAndTitles:@[@"发现"] images:@[@"首页_发现"] color:@[baseColorTheme]];
        _BtnFind.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
        
        [[_BtnFind addTag]subscribeNext:^(id  _Nullable x) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Main_Title_View_WX_BtnNew"];
            self.BtnNew.hidden = YES;
            MainTopicViewController *vc = [[MainTopicViewController alloc]init];
            [self.getNav pushViewController:vc animated:YES];
        }];
        _BtnFind.backgroundColor = [UIColor clearColor];

    }
    return _BtnFind;
}

-(BaseBtn *)BtnNew{
    if (!_BtnNew) {
        _BtnNew = [BaseBtn initCustomBtn:@[@"首页_new"] ];
    }
    return _BtnNew;
}

@end
